library(readit)
library(ggplot2)
library(smacof)

#creating functions for MDS plot rotations
cos.deg <- function(x)cos((x*pi)/180)
sin.deg <- function(x)sin((x*pi)/180)

## Plot for City MDS Example [FIGURE 1]##

cities <- readit("E:/Dropbox/Affective_Polarization_Cross_Country/european_cities.xls")

cities.MDS <- smacofSym(cities, type = "ordinal")

hold <- cities.MDS$conf
hold[,2] <- hold[,2]*-1

A <- matrix(c(cos.deg(-50), sin.deg(-50),
              -sin.deg(-50), cos.deg(-50)), nrow=2, ncol=2)
#
cities.mds.dat <- as.data.frame(hold %*% A)

cities.mds.dat$city <- c("London","Stockholm","Lisboa","Madrid","Paris","Amsterdam","Berlin","Prague","Rome","Dublin")

map.table <- data.frame(City = c("Lon-\ndon","Stock-\nholm","Lis-\nboa","Madrid","Paris","Amst-\nerdam","Berlin","Prague","Rome","Dublin"),cities)

colnames(map.table) <- c("City",map.table$City)

#plain graph#
set.seed(1995)
city.plot <- ggplot(data = cities.mds.dat, aes(x = V1, y = V2, label = city)) + 
  geom_point() + geom_label_repel(size = 3) +
  ggtitle(#"USE ARROW SYMBOL HERE"#, 
    subtitle = "\nMDS City Configuration Plot\n") + 
  coord_fixed(xlim = c(-2.2, 2.2), ylim = c(-1.1,1.1)) +
  theme_few() + theme(axis.title = element_blank(),plot.title = element_text(hjust = 0.5,size=30), plot.subtitle = element_text(hjust = 0.5, size=15))

city.plot

#table graph#

city.table.plot <- ggplot() +
  annotate(geom = "table", x = 0, y = 1000, label = list(map.table),size = 3) + ggtitle("Flight Distance Table") +
  theme_void() + 
  theme(axis.title = element_blank(), plot.title = element_text(hjust = 0.5,size=15))

#city.table.plot

city.plot.final <- ggarrange(city.table.plot,city.plot, ncol = 1, nrow = 2, align = "h")

city.plot.final
